package top.abrsm.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import top.abrsm.entity.*;
import top.abrsm.entity.bo.ExpressBo;
import top.abrsm.entity.bo.ExpressContentBo;
import top.abrsm.service.IAddressService;
import top.abrsm.service.IExpressService;
import top.abrsm.service.IFileService;
import top.abrsm.service.IProductService;
import top.abrsm.util.HttpUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping ("express")
@Controller
public class ExpressController {

    @Resource
    private IExpressService iExpressService;
    @Resource
    private IProductService iProductService;
    @Resource
    private IFileService iFileService;
    @Resource
    private IAddressService iAddressService;

    @RequestMapping ("querySingle")
    public String querySingle (@Param ( "orderId" ) String orderId, HttpSession session, Model model) {
        StudentEntity studentEntity = (StudentEntity) session.getAttribute ( "studentEntity" );
        ExpressEntity expressEntity = new ExpressEntity ();
        expressEntity.setOrderId ( orderId );
        expressEntity = iExpressService.querySingle ( expressEntity );
        // 查询商品和图片
        ProductEntity productEntity = new ProductEntity ();
        productEntity.setId ( expressEntity.getProductId () );
        productEntity = iProductService.querySingle ( productEntity );
        FileEntity fileEntity = new FileEntity ();
        fileEntity.setFileJoinId ( productEntity.getId () );
        fileEntity.setFileJoinType ( 2 );
        List<FileEntity> fileEntities = iFileService.queryList ( fileEntity );
        productEntity.setFileEntities ( fileEntities );
        model.addAttribute ( "productEntity", productEntity );
        AddressEntity addressEntity = new AddressEntity ();
        addressEntity.setIsDefault ( 1 );
        addressEntity.setStuId ( studentEntity.getId () );
        addressEntity = iAddressService.querySingle ( addressEntity );
        model.addAttribute ( "addressEntity", addressEntity );
        String host = "https://ali-deliver.showapi.com";
        String path = "/showapi_expInfo";
        String method = "GET";
        String appcode = "93aefc96a70d4396b0b0f8361889eb1d";
        Map<String, String> headers = new HashMap<String, String> ();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put ( "Authorization", "APPCODE " + appcode );
        Map<String, String> querys = new HashMap<String, String> ();
        //快递公司字母简称,可以从接口"快递公司查询" 中查到该信息 可以使用"auto"代替表示自动识别,不推荐大面积使用auto，建议尽量传入准确的公司编码。
        querys.put ( "com", "auto" );
        //快递单号
        querys.put ( "nu", expressEntity.getOddNumbers () );
        //收/寄件人手机号后四位，顺丰快递必须填写本字段。
        querys.put ( "receiverPhone", expressEntity.getStuMobile ().substring ( 7 ) );
        querys.put ( "senderPhone", "18156023057" );
        try {
            HttpResponse response = HttpUtils.doGet ( host, path, method, headers, querys );
            //获取response的body
            String s = EntityUtils.toString ( response.getEntity () );
            // 解析快递记录
            ExpressBo parse = JSONObject.parseObject ( s, ExpressBo.class );
            JSONObject showapi_res_body = parse.getShowapi_res_body ();
            String expTextName = showapi_res_body.getString ( "expTextName" );
            String data = showapi_res_body.getString ( "data" );
            JSONArray jsonArray = JSONArray.parseArray ( data );
            List<ExpressContentBo> expressContentBos = JSONObject.parseArray ( jsonArray.toString (), ExpressContentBo.class );
            expressEntity.setExpressName ( expTextName );
            expressEntity.setExpressContentBos ( expressContentBos );
            model.addAttribute ( "expressEntity", expressEntity );
        } catch ( Exception e ) {
            e.printStackTrace ();
        }
        return "expressInfo";
    }


}
